<?php
namespace Home\Controller;
use Think\Controller;

class UserController extends CommonController {
    static function cl_sms($mobile, $content,$shop_identifier)
    {
        date_default_timezone_set('PRC');//设置时区
        $url = 'https://smssh1.253.com/msg/send/json';
        $data['account'] = 'N3075640';
        $data['password'] = 'C8gWliKHwneaf5';
        $app_name = M('merchant','',C('DB2'))->where(['shop_identifier'=>$shop_identifier])->getField('merchant_name');
        $data['msg'] = '【'.$app_name.'】' . $content;
//        $data['msg'] = '【信用查】' . $content;
        $data['phone'] = $mobile;
        $data['sendtime'] = date('YmdHi');
        $data['report'] = 'true';
        $header = array('Content-Type:application/json;charset=utf-8');
        $result = self::set_curl($url, json_encode($data), $header);
        $res = json_decode($result, true);
        if ($res['code'] == '0') {
            $add_news = array(
                'shop_identifier'=>$shop_identifier,
                'mobile'=>$mobile,
                'result'=>'1001',
                'ctime'=>time(),
                'content'=>$content,
            );
            M('user_sms_record','',C('DB2'))->add($add_news);
            return true;
        } else {
            $add_news = array(
                'shop_identifier'=>$shop_identifier,
                'mobile'=>$mobile,
                'result'=>$res['errorMsg'],
                'ctime'=>time(),
                'content'=>$content,
            );
            M('user_sms_record','',C('DB2'))->add($add_news);
            return $res['errorMsg'];
        }
    }
    public function Charging($shop_identifier,$mobile,$detail){
        $merchant = M('merchant','',C('DB2'))->field("consume_type,consume_value")->where(array('shop_identifier'=>$shop_identifier))->find();
        $consume_type = $merchant['consume_type'];
        $consume_value = $merchant['consume_value']*0.01;
        $price = M('price','',C('DB2'))->where(array('id'=>1))->getField("money");
        if($consume_type==2){
            $money = $price*$consume_value;
        }elseif($consume_type==3){
            $money = $price*2;
        }else{
            $money = $price;
        }
        $url = "http://dnqxfv2.hzdouniu.cn/haoxin/deducted";
        $add= array(
            'shopIdentifier'=>$shop_identifier,
            'mobile'=>$mobile,
            'money'=>$money,
            'detail'=>$detail,
            'type'=>'1',
            'costType'=>'1',
        );

        $result = $this->curl_post_https($url,$add);
        $result = json_decode($result,true);
        return $result;
    }
    public function set_curl($url = '',$data='',$header='')
    {

        $ch = curl_init();
        if (!empty($header)) {
//            $header=array('Content-type: application/json','charset:utf-8'); 示例
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
        curl_setopt($ch, CURLOPT_URL, $url);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, '1');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //禁用后cURL将终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $res = curl_exec($ch);
        if (curl_error($ch)) {
            return '错误信息:' . curl_error($ch);
        }
        curl_close($ch);
        return $res;
    }
    public function user_info(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id = $get['user_id'];
        if(empty($user_id)){
            $data['ret']='1001';
            $data['code']='success';
            $data['data']['username']='未登录';
            $data['data']['status']='0';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
        $user_data = M('user','',C('DB2'))->where(array('id'=>$user_id))->field('username,cert_status')->find();
        if(empty($user_data['username'])){
            $data['ret']='1001';
            $data['code']='success';
            $data['data']['username']='未认证';
            $data['data']['status']='1';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }else{
            $data['ret']='1001';
            $data['code']='success';
            $data['data']['username']=$user_data['username'];
            $data['data']['status']=$user_data['cert_status'];
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
    }
    public function electronic_sign(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id = $get['user_id'];
        $sign_img = $get['sign_img'];
        if(empty($user_id)||empty($sign_img)){
            $data['ret']='1002';
            $data['code']='参数错误，请联系客服';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
        $has = M('user_sign_img','',C('DB2'))->where(array('user_id'=>$user_id,'use_status'=>'2'))->find();
        if(empty($has)){
            $add = array(
                'user_id'=>$user_id,
                'sign'=>md5('xyc'.$user_id.$sign_img.time().rand(000,999)),
                'sign_img'=>$sign_img,
                'use_status'=>'2',
                'ctime'=>time(),
            );
            $res=M('user_sign_img','',C('DB2'))->add($add);
            if($res){
                $data['ret']='1001';
                $data['code']='添加签名成功';
                $data1 = $this->jiami($data);
                $this->return = ['data'=>$data1];
                exit();
            }else{
                $data['ret']='1002';
                $data['code']='系统错误，请联系客服';
                $data1 = $this->jiami($data);
                $this->return = ['data'=>$data1];
                exit();
            }
        }else{
            M('user_sign_img','',C('DB2'))->where(array('user_id'=>$user_id,'use_status'=>'2'))->setField('use_status','1');
            $add = array(
                'user_id'=>$user_id,
                'sign'=>md5('xyc'.$user_id.$sign_img.time().rand(000,999)),
                'sign_img'=>$sign_img,
                'use_status'=>'2',
                'ctime'=>time(),
            );
            $res=M('user_sign_img','',C('DB2'))->add($add);
            if($res){
                $data['ret']='1001';
                $data['code']='更新签名成功';
                $data1 = $this->jiami($data);
                $this->return = ['data'=>$data1];
                exit();
            }else{
                $data['ret']='1002';
                $data['code']='系统错误，请联系客服';
                $data1 = $this->jiami($data);
                $this->return = ['data'=>$data1];
                exit();
            }
        }
    }
    public function get_mobile(){
        $data1=I('data');
        $get=$this->jiemi($data1);
        $user_id = $get['user_id'];
        if(empty($user_id)){
            $data['ret']='1002';
            $data['code']='请先登陆';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
        $mobile = M('user','',C('DB2'))->where(array('id'=>$user_id))->getField('mobile');
        if($mobile){
            $data['ret']='1001';
            $data['code']='success';
            $data['data']['mobile']=$mobile;
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }else{
            $data['ret']='1002';
            $data['code']='error';
            $data['data']['mobile']='';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
    }
    public function check_paypwd(){
        $data1=I('data');
        $get=$this->jiemi($data1);
        $user_id = $get['user_id'];
//        $user_id ='1180';
        $paypwd = md5('hxgj'.$get['paypwd']);
//        $paypwd = md5('hxgj123456789');
        $shop_identifier = $get['shop_identifier'];
//        $shop_identifier = 'xinyongcha';
        if(empty($user_id)||empty($paypwd)){
            $data['ret']='1002';
            $data['code']='参数缺失';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
        $user_paypwd = M('user_login','',C('DB2'))->where(array('user_id'=>$user_id,'shop_identifier'=>$shop_identifier))->getField('paypwd');
        if($user_paypwd!=$paypwd){
            $data['ret']='1002';
            $data['code']='交易密码错误';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }else{
            $data['ret']='1001';
            $data['code']='success';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
    }
    public function user_sign(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id = $get['user_id'];
        $has = M('user_sign_img','',C('DB2'))->where(array('user_id'=>$user_id,'use_status'=>'2'))->find();
        if(empty($has['sign_img'])){
            $data['ret']='1002';
            $data['code']='未发现用户签名';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
        $data['ret']='1001';
        $data['code']='未发现用户签名';
        $data['data']['img'] = $has['sign_img'];
        $data1 = $this->jiami($data);
        $this->return = ['data'=>$data1];
        exit();
    }
    public function test(){
        $data = 'dJ18yMf\/VBBWgym5Jjn7uqdttAZwjX++VFY7ktcNf0Ihs54NqdLeo1+Tj4dvCTPvTuDkPwAukDfGpGvgq7SaTYEZnVqB9Hkue8Scudotq5AqyhWMxK9U1tqPyh9oFcnvAa4waVLuMfW3LObRtTZHmPCI+u0X7PAC9n4CrTmvPQujDDpUgx1AU\/WPjR\/t06gV+WB42vkeZLg=';
        $res= $this->jiemi($data);
        var_dump($res);
    }
    public function pay_pwd_verify(){
        ob_clean();
        $id = I('ss');
        $config =    array(    'fontSize'    =>    50,    // 验证码字体大小
            'length'      =>    3,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'model'=>1,
            'useCurve'=>false,
            'useImgBg'=>false,
        );
        $Verify =     new \Think\Verify($config);
        $Verify->codeSet = '0123456789';
        $ip=get_client_ip();
        $Verify->entry($ip);
    }
    public function editpaypwd_code(){
        header("Content-Type: text/html; charset=UTF-8");
        $data1=I('data');
        $get=$this->jiemi($data1);
        $user_id = $get['user_id'];
//        $user_id = '1158';
        $ss = $get['ss'];
        $verify =     new \Think\Verify();
        $verify->useImgBg = true;
        $verify_code=$get['verify'];
        $mobile = M('user','',C('DB2'))->where(array('id'=>$user_id))->getField('mobile');
        $shop_identifier = $get['shop_identifier'];
        $device = $get['device'];
        $ip=get_client_ip();
//        if($shop_identifier=='0100010020'||$shop_identifier=='0100030016'||$shop_identifier=='0100030015'||$shop_identifier=='0100030018'||$shop_identifier=='0100010003') {
//            //$yzm = new YzmController();
//            $check = $this->verify($verify_code, $ip);//二次验证
//            if (!$check) {
//                $data['ret'] = '1002';
//                $data['code'] = '滑块验证码错误1';
//                $data1 = $this->jiami($data);
//                $this->return = ['data' => $data1];
//                exit;
//            }
//        }else{
//            $check=$verify->check($verify_code,$ip);
//            if(!$check&&$device!='android'){
//                $data['ret']='1002';
//                $data['code']='图片验证码错误2';
//                $data1 = $this->jiami($data);
//                $this->return = ['data'=>$data1];
//                exit;
//            }
//        }
        if($shop_identifier!='0100020003') {
            $check = $this->verify($verify_code, $ip);//二次验证
            if (!$check) {
                $data['ret'] = '1002';
                $data['code'] = '滑块验证码错误';
                $data1 = $this->jiami($data);
                $this->return = ['data' => $data1];
                exit;
            }
        }
        $phonecode=rand(100000,999999);
        //$code=(string)rand(1000,9999);//语音验证码
        //$mobilecode="     $code[0]     $code[1]     $code[2]     $code[3]";//语音验证码/带空格
        $count=S($mobile.'count');
        $time=S($mobile.'editpaypwd_time');
        if(time()-$time>120&&$count<8)
        //if(time()-$time>120)
        {
            $time = 86400-(time()-strtotime(date('Y-m-d',time())));
            S($mobile.'editpaypwd_verify',$phonecode);
            S($mobile.'editpaypwd_time',time());
            $content = '验证码为'.$phonecode.',您正在修改交易密码，请勿将验证码透露给他人。';
            $detail = "注册短信获取费用";
            $result = $this->Charging($shop_identifier,$mobile,$detail);
            if($result['isSuccess']!=0) {
                $status = $this->cl_sms($mobile, $content, $shop_identifier);
                //$status=$this->voice_code($mobile,$mobilecode);
//            if($status)
                if ($status) {
                    S($mobile . 'count', $count + 1, $time);
                    $data['ret'] = '1001';
                    $data['code'] = '短信验证码已成功发送,请注意查收';
                    $data1 = $this->jiami($data);
                    $this->return = ['data' => $data1];
                    exit;
                } else {
                    $data['ret'] = '1002';
                    $data['code'] = '短信验证码发送失败,请联系管理员';
                    $data1 = $this->jiami($data);
                    $this->return = ['data' => $data1];
                    exit;
                }
            }else{
                $data['ret'] = '1002';
                $data['code'] = '发送失败,请联系管理员';
                $data1 = $this->jiami($data);
                $this->return = ['data' => $data1];
                exit;
            }

        }
        else
        {
            $data['ret']='1002';
            $data['code']='短信验证码之间发送间隔为3分钟，且每日只能发送5条';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit;
        }
    }
    function curl_post_https($url,$data){ // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据，json格式

    }
    public function pay_pwd(){
        $get = I('data');
        $get = $this->jiemi($get);
        if(!IS_POST){
            $data['ret']='1002';
            $data['code']='NOT POST';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
        $newpwd = md5('hxgj'.$get['newpwd']);
        $shop_identifier=$get['shop_identifier'];
        $user_id = $get['user_id'];
        $code = $get['code'];
        $mobile = M('user','',C('DB2'))->where(array('id'=>$user_id))->getField('mobile');
        $time =S($mobile.'editpaypwd_time');
        $redcode =S($mobile.'editpaypwd_verify');
        //$user_id!='48'||
        if($mobile!='15225170185'){
            if(time()-$time>120||$redcode!=$code){
                //if($redcode!=$code){
                $data['ret']='1002';
                $data['code']='验证码错误';
                $data = $this->jiami($data);
                $this->return = ['data'=>$data];
                exit;
            }
        }

        S($mobile.'editpaypwd_time',null);
        S($mobile.'editpaypwd_verify',null);
        $model = M('user_login','',C('DB2'));
        $res = $model->where(array('user_id'=>$user_id,'shop_identifier'=>$shop_identifier))->save(array('paypwd'=>$newpwd));
        if($res||$res!==false){
            $mobile = M('user','',C('DB2'))->where(array('id'=>$user_id))->getField('mobile');
            $content = '您已经通过手机号验证进行交易密码修改，原密码将不能使用。如有疑问请致电客服。';
            $detail = "交易密码修改设置短信提醒";
            $result = $this->Charging($shop_identifier,$mobile,$detail);
            if($result['isSuccess']!=0) {
                $this->cl_sms($mobile, $content, $shop_identifier);
            }
            $data['ret']='1001';
            $data['code']='修改成功';
            $data = $this->jiami($data);
            $this->return = ['data'=>$data];
            exit();
        }
    }
    public function personal(){
        $get = I('data');
        $get = $this->jiemi($get);
        $vid = $get['user_id'];
        $user_data = M('user','',C('DB2'))->where(array('id'=>$vid))->field('mobile,username as name,identity_num,score,evaluat_status')->find();
        if(empty($vid)){
            $user_data['name'] = '未登录';
        }elseif($user_data['evaluat_status']=='1'){
            $user_data['name'] = '未认证';
        }
        $user_data['name']=empty($user_data['name'])?'':$user_data['name'];
        $user_data['mobile']=empty($user_data['mobile'])?'':$user_data['mobile'];
        $user_data['id_card']=empty($user_data['id_card'])?'':$user_data['id_card'];
        $list = array(
            '0'=>array('url'=>'xgmm','content'=>'修改密码','ishead'=>'1','islogin'=>'1'),
            '1'=>array('url'=>'https://hxgj.dstlkj.com/Index/index#/feedback','content'=>'意见反馈','ishead'=>'1','islogin'=>'1'),
            '2'=>array('url'=>'https://hxgj.dstlkj.com/Index/index#/about','content'=>'关于我们','ishead'=>'1','islogin'=>'1'),
            '3'=>array('url'=>'tcdl','content'=>'退出登陆','ishead'=>'1','islogin'=>'1'),
        );
        $isnull = empty($user_data)?'1':'0';
        $data['ret']='1001';
        $data['code']='success';
        $data['isnull']=$isnull;
        $data['data']['user_data']=$user_data;
        $data['data']['page_data']=$list;
        $data1 = $this->jiami($data);
        $this->return = ['data'=>$data1];
        exit();
    }
    public function jm(){
        $data = I('data');
        $data = $this->jiemi($data);
        var_dump($data);
    }
    /*
     * 用户反馈
     */
    public function feedback(){
        $get = I('data');
        $get = $this->jiemi($get);
        $vid = $get['user_id'];
        $content = $get['content'];
        $img = $get['img'];
        if(empty($content)){
            $data['ret']='1002';
            $data['code']='反馈内容不得为空';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
        $new_time = M('user_feedback','','DB2')->where(array('user_id'=>$vid))->order('ctime desc')->getField('ctime');
        if(time()-$new_time<60){
            $data['ret']='1002';
            $data['code']='反馈间隔不得短于一分钟';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
        $len = mb_strlen($content);
        if($len>200){
            $data['ret']='1002';
            $data['code']='反馈字数不得超过200';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
//        $add = array(
//            'user_id'=>$vid,
//            'content'=>$content,
//            'ctime'=>time(),
//            'img'=>$img
//        );
        $add["user_id"]=$vid;
        $add["content"]=$content;
        $add["ctime"]=time();
        $add["img"]=$img;
        $res = M('user_feedback','','DB2')->add($add);
        if($res){
            $data['ret']='1001';
            $data['code']='反馈成功';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }else{
            $data['ret']='1002';
            $data['code']='反馈失败';
            $data1 = $this->jiami($data);
            $this->return = ['data'=>$data1];
            exit();
        }
    }

    public function hwpush(){
        $get = I('data');
        $get = $this->jiemi($get);
        $vid = $get['vid']; //用户id
        $message = $get['message'];//发送内容
        $redis = new \Redis();
        $redis->connect('localhost', 6379);
        $reid= $redis->get('hw'.$vid);
//        var_dump(S('a'));die;
        if(!IS_POST) {
            $this->ajaxReturn(array('ret'=>'1002','code'=>'NOT POST'))  ;
            exit();
        }
        if(empty($vid)) {
            $this->ajaxReturn(array('ret'=>'1002','code'=>'用户不存在')) ;
            exit();}

        if(empty($message))
        {
            $this->ajaxReturn(array('ret'=>'1002','code'=>'内容不能为空')) ;
            exit();
        }
//        if(empty($uid)){
//            $this->ajaxReturn(array('ret'=>'1002','code'=>'uid不能为空')) ;
//        }
//        var_dump(S('hw'.$vid));die;
//        var_dump(S('hw'.$vid));die;
        Vendor('Hwpush.Push');
        $Hwpush=new \Hwpush\Push();
        $hw=$Hwpush->testSendNotice($reid,'这是一条华为推送');//华为vid
//        var_dump($hw);die;
        $mobile = M('user','',C('DB'))->where(array('vid'=>$vid))->getField('mobile');
        Vendor('Sdk.Push');
        $xmpush = new \Sdk\Push();
        //  $xm_regid=S('xm'.$uid);
        $xm_regid=$mobile;//别名
        $res = $xmpush->pushs(3,$xm_regid,$message,'这是一条小米推送','');
//        var_dump($res);die;
        Vendor('Pushsdk.Push');
        $mzPushSKD=new \Pushsdk\Push();
        $mzpush =$mobile;
        $mz = $mzPushSKD->pushs(3,$mzpush,$message,'这是一条魅族推送');
        var_dump($mz);

        return $hw;
    }
    public function push(){
        $this->mypush('下午好','0bd010b04c4678b77e40b1f50a40e91f');
    }
    public function push_login(){
        $get=$this->_callback;
        $regid = $get['regid'];
        $vid = $get['vid'];
        $type=$get['type'];
        $redis = new \Redis();
        $redis->connect('localhost', 6379);
        if(!IS_POST) {
            $data1['ret']='1002';
            $data1['code']='NOT POST';
            $this->return = $data1;
            exit;
        }else if(empty($vid))
        {
            $data1['ret']='1002';
            $data1['code']='用户未登录';
            $this->return = $data1;
            exit;
        }
        if($type=='1'){
            $redis->set('hw'.$vid,$regid);
            $hw=$redis->get('hw'.$vid);
            if(empty($hw)){
                $data1['ret']='1002';
                $data1['code']='error';
                $this->return = $data1;
                exit;

            }
            $data1['ret']='1001';
            $data1['code']='success';
            $data1['data']['rid']=$hw;
            $this->return = $data1;
            exit;
        }else{
            $data1['ret']='1001';
            $data1['code']='success';
            $this->return = $data1;
            exit;
        }
    }

    public function user_mobile_book(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id= $get['user_id'];
        $data = M('mobilebook','',C('NTH'))->where(array('user_id'=>$user_id))->field('mail_name,mail_mobile')->select();
        $data1['ret']='1001';
        $data1['code']='成功';
        $data1['isnull'] = empty($data)?'1':'0';
        $data1['data']['data']=$data;
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit();
    }

    public function mobile_book_status(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id= $get['user_id'];
        if(empty($user_id)){
            $data1['ret']='1001';
            $data1['code']='反馈成功';
            $data1['isnull'] = '1';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        $data1['ret']='1001';
        $data1['code']='反馈成功';
        $data1['isnull'] = '0';
        $data1['data']['data']=array(
            '0'=>array('mon'=>'3','num'=>rand(10,30)),
            '1'=>array('mon'=>'2','num'=>rand(10,30)),
            '2'=>array('mon'=>'1','num'=>rand(10,30)),
            '3'=>array('mon'=>'12','num'=>rand(10,30)),
            '4'=>array('mon'=>'11','num'=>rand(10,30)),
            '5'=>array('mon'=>'10','num'=>rand(10,30)),
        );
        $data1['data']['status']='1';
        $data1['data']['mark']='轻度影响';
        $data1['data']['content']='社交圈联系密切且较为稳定';
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit();
    }

    public function bad_message(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id= $get['user_id'];
        if(empty($user_id)){
            $data1['ret']='1001';
            $data1['code']='反馈成功';
            $data1['isnull'] = '1';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        $data1['ret']='1001';
        $data1['code']='反馈成功';
        $data1['isnull'] = '1';
        $data1['data']['num']='0';
        $data1['data']['content']='';
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit();
    }
    public function bad_borrow(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id= $get['user_id'];
        if(empty($user_id)){
            $data1['ret']='1001';
            $data1['code']='反馈成功';
            $data1['isnull'] = '1';
            $data1 = $this->jiami($data1);
            $this->return = ['data'=>$data1];
            exit();
        }
        $data1['ret']='1001';
        $data1['code']='反馈成功';
        $data1['isnull'] = '1';
        $data1['data']['num']='0';
        $data1['data']['content']='';
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit();
    }
    public function search_friend(){
        $get = I('data');
        $get = $this->jiemi($get);
        $mobile= $get['mobile'];
        $user = M('user','',C('DB2'))->where(array('mobile'=>$mobile))->field('username as name,identity_num')->find();
        $data1['ret']='1001';
        $data1['code']='成功';
        $data1['isnull'] = empty($user)?'1':'0';
        $data1['data']['data']=$user;
        $data1 = $this->jiami($data1);
        $this->return = ['data'=>$data1];
        exit();
    }
    public function friend_message(){

    }
    function mypush($content, $vid = '', $title = '系统信息')
    {

        $client = new \Home\Service\JPush\Client('19e30bd4929dbb6d59f94c2b', '07aabf8c871cb2603fe4bf28');//安卓
        $client_ios = new \Home\Service\JPush\Client('fee054dac56b75b83fe55cad', '3e340cef39f1ed14419b9b57');//ios
        $result = $client
            ->push()
            ->setPlatform(array('android', 'ios'))
            ->options(['apns_production' => true]);
        $result_ios = $client_ios
            ->push()
            ->setPlatform(array('android', 'ios'))
            ->options(['apns_production' => true]);
        //options参数，true表示生产环境，文档里写的默认是生产环境是有错误的，必须手动设置
        //vid为空的话表示群发
        if (empty($vid)) {
//            $result->addAllAudience();
        } else {
            $result->addAlias(trim($vid));
        }
        if (empty($vid)) {
//            $result_ios->addAllAudience();
        } else {
            $result_ios->addAlias(trim($vid));
        }
        if (empty($vid)) {
            $type = '2';
        } else {
            $type = '1';
        }
        $redis = new \Redis();
        $redis->connect('localhost', 6379);
        $reid= $redis->get('hw'.$vid);
        Vendor('Hwpush.Push');
        $Hwpush=new \Hwpush\Push();
        $hw=$Hwpush->testSendNotice($reid,$content);//华为vid
        $mobile = M('user','',C('DB'))->where(array('vid'=>$vid))->getField('mobile');
        Vendor('Sdk.Push');
        $xmpush = new \Sdk\Push();
        $xm_regid=$mobile;//别名
        $res = $xmpush->pushs(3,$xm_regid,$content,'系统信息','');
        Vendor('Pushsdk.Push');
        $mzPushSKD=new \Pushsdk\Push();
        $mzpush =$mobile;
        $mz = $mzPushSKD->pushs(3,$mzpush,$content,'系统信息');
//        $adddata = array(
//            'title' => $title,
//            'content' => $content,
//            'ctime' => time(),
//            'status' => '1',
//            'vid' => $vid,
//            'type' => $type,
//        );
//        M('user_news', '', C('DB'))->add($adddata);
        //ios端是没有标题的，只有安卓端才有标题
        $res = $result
            ->iosNotification($content, [
                'sound' => 'sound',
                'badge' => '+1',
                'extras' => [
                    'key' => 'value'
                ]
            ])
            ->androidNotification($content, [
                'title' => $title,
                'extras' => [
                    'key' => 'value'
                ]
            ])
            ->send();
        $ress = $result_ios
            ->iosNotification($content, [
                'sound' => 'sound',
                'badge' => '+1',
                'extras' => [
                    'key' => 'value'
                ]
            ])
            ->androidNotification($content, [
                'title' => $title,
                'extras' => [
                    'key' => 'value'
                ]
            ])
            ->send();
    }
    /*
      * 个人信息详情
      */
    public function personal_info(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id=$get["user_id"];///用户唯一标识
        if (empty($get["user_id"])){
            $data['ret'] = '1002';
            $data['code'] = '请先登入';
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
            return false;
        }
        $t_user=M('user',Null,'DB2')->where(["id"=>$user_id])->field("id,username,mobile,identity_num,wechat,address,backup_phone,qq_number")->find();
        //$t_user["identity_num"]= substr($t_user["identity_num"], 0, 5).'*******'.substr($t_user["identity_num"], 12);
        $t_user["identity_num"]=empty($t_user["identity_num"])?"":substr($t_user["identity_num"], 0, 5).'*******'.substr($t_user["identity_num"], 12);
        $t_user["card_address"]=$t_user["address"];
        if ($t_user){
            $data['ret'] = '1001';
            $data['code'] = 'success';
            $data['data']=$t_user;
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
            return true;
        }else{
            $data['ret'] = '1002';
            $data['code'] = '数据异常';
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
            return false;
        }
    }
    /*
     * 全部信息修改
     */
    public function card_updata(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id=$get["user_id"];///用户唯一标识
        if (empty($get["user_id"])){
            $data['ret'] = '1002';
            $data['code'] = '请先登入';
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
            return false;
        }
        if(empty($get["qq_number"])&&empty($get["wechat"])&&empty($get["backup_phone"])){
            $data['ret'] = '1002';
            $data['code'] = '修改内容不能为空';
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
            return false;
        }
//   if(empty($get["username"])&&empty($get["identity_num"])&&empty($get["card_address"])
//       &&empty($get["qq_number"])&&empty($get["wechat"])&&empty($get["backup_phone"])){
//                $data['ret'] = '1002';
//                $data['code'] = '修改内容不能为空';
//                $data1 = $this->jiami($data);
//                $this->return = ['data' => $data1];
//                return false;
//            }

//            if(!empty($get["username"])){
//                $data["username"]=$get["username"];///姓名
//            }
//            if(!empty($get["identity_num"])) {
//                if(!preg_match("/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/",$get["identity_num"])) {
//                    $data['ret'] = '1002';
//                    $data['code'] = '身份证号码格式错误';
//                    $data1 = $this->jiami($data);
//                    $this->return = ['data' => $data1];
//                    return false;
//                }
//                $data["identity_num"]=$get["identity_num"];///身份证
//            }
//            if(!empty($get["card_address"])){
//                $data["address"]=$get["card_address"];///详情地址
//            }
            if(!empty($get["qq_number"])){
                $data["qq_number"]=$get["qq_number"];//QQ号
            }
            if(!empty($get["wechat"])){
                $data["wechat"]=$get["wechat"];//微信号
            }
            if(!empty($get["backup_phone"])){
                if(!preg_match("/1[345678]{1}\d{9}$/",$get["backup_phone"])){
                    $data['ret']='1002';
                    $data['code']='手机号码格式有误';
                    $data1 = $this->jiami($data);
                    $this->return = ['data'=>$data1];
                    return false;
                }
                $data["backup_phone"]=$get["backup_phone"];///备用手机号
            }

        $t_user=M('user',Null,'DB2')->where(["id"=>$user_id])->save($data);
        if ($t_user){
            $data['ret'] = '1001';
            $data['code'] = 'success';
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
            return true;
        }else{
            $data['ret'] = '1002';
            $data['code'] = '修改失败';
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
            return false;
        }

    }
    /*
     * 个人信息修改
     */
    public function personal_infoupdata(){
        $get = I('data');
        $get = $this->jiemi($get);
        $user_id=$get["user_id"];///用户唯一标识
        if (empty($get["user_id"])){
            $data['ret'] = '1002';
            $data['code'] = '请先登入';
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
        }
        if(!empty($get["qq_number"])){
            $data["qq_number"]=$get["qq_number"];//QQ号
        }
        if(!empty($get["wechat"])){
            $data["wechat"]=$get["wechat"];//微信号
        }
        if(!empty($get["backup_phone"])){
            $data["backup_phone"]=$get["backup_phone"];///备用手机号
        }
        if(empty($get["backup_phone"])&&empty($get["backup_phone"])&&empty($get["backup_phone"])){
            $data['ret'] = '1002';
            $data['code'] = '修改内容不能为空';
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
        }
        $t_user=M('user',Null,'DB2')->where(["id"=>$user_id])->save($data);
        if($t_user){
            $data['ret'] = '1001';
            $data['code'] = 'success';
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
        }else{
            $data['ret'] = '1002';
            $data['code'] = '修改失败';
            $data1 = $this->jiami($data);
            $this->return = ['data' => $data1];
        }
    }
    /*
   * 语音验证码
   * $mobile 手机号
   */
    public function voice_code($mobile,$code){
//        $code=(string)rand(1000,9999);
//        $mobilecode="$code[0] $code[1] $code[2] $code[3]";
//        S($mobile.'phonechang_verify',$code);
//        S($mobile.'phonechang_time',time());
        $url="http://xycshop.dstlkj.com/ChatApi/voice_code?phone=$mobile&code=".$code;
        $header = array('Content-Type:application/json;charset=utf-8');
        $result = self::get_curl($url,'',$header);
        return json_decode($result,true);
//        if($result['code']=='1001'){
//            $data['ret']='1001';
//            $data['code']='语音验证码已成功发送,请注意接听';
//            echo json_encode($data);
//            exit;
//        }else{
//            $data['ret']='1002';
//            $data['code']='发送失败,请联系管理员';
//            echo json_encode($data);
//            exit;
//        }
    }
    /**
     * 发起二次校b25384ea5b4c4d10ac879889a7dde99c 验请求
     * @param $validate 二次校验数据
     * @param $user 用户信息
     */

    public function verify($validate, $user) {
        $params = array();
        $params["captchaId"] = 'b25384ea5b4c4d10ac879889a7dde99c';
        $params["validate"] = $validate;
        $params["user"] = $user;
        // 公共参数
        $params["secretId"] = '64e7487ba293a4c5013e8e32d284fc64';
        $params["version"] = 'v2';
        $params["timestamp"] = sprintf("%d", round(microtime(true)*1000));// time in milliseconds
        $params["nonce"] = sprintf("%d", rand()); // random int
        $params["signature"] = $this->sign('11d506f8f7d3c60d42437f24d1641f0c', $params);

        //var_dump($params);
        $result = $this->send_http_request($params);
        return array_key_exists('result', $result) ? $result['result'] : false;
    }

    /**
     * 计算参数签名
     * @param $secret_key 密钥对key
     * @param $params 请求参数
     */
    private function sign($secret_key, $params){
        ksort($params); // 参数排序
        $buff="";
        foreach($params as $key=>$value){
            $buff .=$key;
            $buff .=$value;
        }
        $buff .= $secret_key;
        return md5($buff);
    }

    /**
     * 发送http请求
     * @param $params 请求参数
     */
    private function send_http_request($params){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://c.dun.163yun.com/api/v2/verify');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, YIDUN_CAPTCHA_API_TIMEOUT);
        curl_setopt($ch, CURLOPT_TIMEOUT, YIDUN_CAPTCHA_API_TIMEOUT);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

        /*
         * Returns TRUE on success or FALSE on failure.
         * However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.
         */
        $result = curl_exec($ch);
        // var_dump($result);

        if(curl_errno($ch)){
            $msg = curl_error($ch);
            curl_close($ch);
            return array("error"=>500, "msg"=>$msg, "result"=>false);
        }else{
            curl_close($ch);
            return json_decode($result, true);
        }
    }
}